package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import se.perp.c4.control.iface.IReportCreator;
import se.perp.c4.model.Card;
import se.perp.c4.model.Card.ECard;
import se.perp.c4.model.Deck;
import static se.perp.c4.model.Actor.Side.*;

class InfoPanel extends JPanel {

    private Deck myDeck,  opponentDeck;
    private Map<ECard, ImagePanel> myCardPanels = new HashMap<ECard, ImagePanel>();
    private Map<ECard, ImagePanel> opponentCardPanels = new HashMap<ECard, ImagePanel>();
    private final Font cardFont = new Font(Font.SERIF, Font.BOLD, 14);
    private final Font labelFont = FontSet.INFOPANEL_TEXT;
    private JButton reportButton = new JButton("Who's left?");
    private final int CARD_W = 30,  CARD_H = 40;
    private final int CARD_TOP_Y = 30;
    private final int CARD_MYDECK_X = 30;
    private final int CARD_OPPDECK_X = CARD_MYDECK_X + 10 * CARD_W;
    private IReportCreator reportCreator = null;
    private int myScore = 0,  opponentScore = 0;
//    private final ImagePanel connectionOK,  connectionBad;
//    private ImagePanel connectionIndicator;
    private String playerName = "You",  opponentName = "Opponent";
    private final Color youTextColor = new Color(255, 255, 127);
    private final Color oppTextColor = new Color(255, 127, 127);
    private Image bgImage = null;

    public InfoPanel() {
        setLayout(null);
        setSize(-1, 80);
        setPreferredSize(getSize());

        reportButton.setSize(140, 25);
        reportButton.setMnemonic('W');
        reportButton.setEnabled(false);
        reportButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (reportCreator != null) {
                    reportCreator.showReport();
                }
            }
        });
        add(reportButton);

//        connectionOK = new ImagePanel(Images.get(Images.Which.CONNECTION_OK));
//        connectionOK.setToolTipText("You are connected.");
//        connectionOK.setVisible(false);
//        connectionOK.setOpaque(false);
//
//        connectionBad = new ImagePanel(Images.get(Images.Which.CONNECTION_LOST));
//        connectionBad.setToolTipText("No connection.");
//        connectionBad.setVisible(false);
//        connectionBad.setOpaque(false);
//
//        connectionIndicator = connectionBad;
//
//        add(connectionBad);
//        add(connectionOK);

        setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    void setBgImage(Image img) {
        bgImage = img;
    }

    @Override
    public void validate() {
        super.validate();
        int bx = getWidth() - reportButton.getWidth() - 10;
        int by = CARD_TOP_Y + CARD_H - reportButton.getHeight();
        reportButton.setLocation(bx, by);

//        int lx = bx - connectionBad.getWidth() - 10;
//        int ly = by + (reportButton.getHeight() - connectionBad.getHeight()) / 2;
//        connectionBad.setLocation(lx, ly);
//        connectionOK.setLocation(lx, ly);

        refitCards();
    }

    void setMyDeck(Deck d) {
        myDeck = d;
        if (d != null) {
            for (ImagePanel p : myCardPanels.values()) {
                remove(p);
            }
            myCardPanels.clear();
            for (Card c : d.getDeck()) {
                ImagePanel p = new ImagePanel(createCardImage(c));
                p.setHoverSensitive(false);
                p.setToolTipText("<html><body><center><b>" + c.name + "</b></center>" + c.text + "</body></center>");
                myCardPanels.put(c.type, p);
                add(p);
            }
        }
        refitCards();
    }

    void setOpponentDeck(Deck d) {
        opponentDeck = d;
        if (d != null) {

            for (ImagePanel p : opponentCardPanels.values()) {
                remove(p);
            }
            opponentCardPanels.clear();
            for (Card c : d.getDeck()) {
                ImagePanel p = new ImagePanel(createCardImage(c));
                p.setHoverSensitive(false);
                p.setToolTipText("<html><body><center><b>" + c.name + "</b></center>" + c.text + "</body></center>");
                opponentCardPanels.put(c.type, p);
                add(p);
            }
        }
        refitCards();
    }

    void setMyScore(int myScore) {
        this.myScore = myScore;
    }

    void setOpponentScore(int opponentScore) {
        this.opponentScore = opponentScore;
    }

    void setReportButtonEnabled(boolean b) {
        reportButton.setEnabled(b);
    }

    void setReportCreator(IReportCreator rc) {
        reportCreator = rc;
    }

    private void refitCards() {
        int x = CARD_MYDECK_X;
        int y = CARD_TOP_Y;

        for (ImagePanel p : myCardPanels.values()) {
            p.setVisible(false);
        }
        for (ImagePanel p : opponentCardPanels.values()) {
            p.setVisible(false);
        }

        if (myDeck != null) {
            for (Card c : myDeck.getDeck()) {
                ImagePanel p = myCardPanels.get(c.type);

                p.setLocation(x, y);
                p.setVisible(true);
                x += CARD_W;
            }
        }

        x = CARD_OPPDECK_X;

        if (opponentDeck != null) {
            for (Card c : opponentDeck.getDeck()) {
                ImagePanel p = opponentCardPanels.get(c.type);
                p.setLocation(x, y);
                p.setVisible(true);
                x += CARD_W;
            }
        }
        repaint();
    }

    private BufferedImage createCardImage(Card card) {
        BufferedImage img = new BufferedImage(CARD_W, CARD_H, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = (Graphics2D) img.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRoundRect(0, 0, CARD_W, CARD_H, 5, 5);
        g.setColor(Color.BLACK);
        g.drawRoundRect(0, 0, CARD_W - 1, CARD_H - 1, 5, 5);

        Color bg = (card.side == DARK ? ColorSet.DARKCARD_BG : ColorSet.LIGHTCARD_BG);
        g.setColor(bg);
        g.fillRoundRect(3, 3, CARD_W - 6, CARD_H - 6, 5, 5);

        Color fg = (card.side == DARK ? ColorSet.DARKCARD_TEXT : ColorSet.LIGHTCARD_TEXT);
        g.setColor(fg);

        g.setFont(cardFont);
        String s = card.stringRep;
        int strw = g.getFontMetrics().stringWidth(s);
        int strh = g.getFontMetrics().getAscent();
        int x = (30 - strw) / 2;
        int y = (40 - strh);

        g.drawString(s, x, y);

        return img;
    }

    @Override
    protected void paintComponent(Graphics g_) {

        if (bgImage != null) {
            g_.drawImage(bgImage, 0, 0, null);
        }

        Graphics2D g = (Graphics2D) g_;

        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        String t1 = "Your cards:", t2 = opponentName + "'s cards:";

        g.setFont(labelFont);
        int t1x = CARD_MYDECK_X;
        int t1y = CARD_TOP_Y - 5;

        int t2x = CARD_OPPDECK_X;
        int t2y = t1y;

        g.setColor(Color.BLACK);
        g.drawString(t1, t1x + 1, t1y + 1);
        g.setColor(Color.WHITE);
        g.drawString(t1, t1x, t1y);

        g.setColor(Color.BLACK);
        g.drawString(t2, t2x + 1, t2y + 1);
        g.setColor(Color.WHITE);
        g.drawString(t2, t2x, t2y);

        String t3 = playerName + ": " + myScore, t4 = opponentName + ": " + opponentScore;

        int t3x = getWidth() - g.getFontMetrics().stringWidth(t3) - 10;
        int t4x = getWidth() - g.getFontMetrics().stringWidth(t4) - 10;
        int t3y = g.getFontMetrics().getHeight();
        int t4y = t3y + g.getFontMetrics().getHeight();

        g.setColor(Color.BLACK);
        g.drawString(t3, t3x + 1, t3y + 1);
        g.setColor(youTextColor);
        g.drawString(t3, t3x, t3y);

        g.setColor(Color.BLACK);
        g.drawString(t4, t4x + 1, t4y + 1);
        g.setColor(oppTextColor);
        g.drawString(t4, t4x, t4y);

    }

//    void setConnectionIndicatorVisible(boolean b) {
//        connectionIndicator.setVisible(b);
//    }
//
//    void setConnectionIndicatorOK() {
//        connectionOK.setVisible(true);
//        connectionBad.setVisible(false);
//        connectionIndicator = connectionOK;
//    }
//
//    void setConnectionIndicatorBad() {
//        connectionOK.setVisible(false);
//        connectionBad.setVisible(true);
//        connectionIndicator = connectionBad;
//    }

    void setPlayerName(String name) {
        playerName = name;

    }

    void setOpponentName(String name) {
        opponentName = name;
    }
}
